#pragma once
#include "../source/server.hpp"

class EchoServer
{
private:
    TcpServer _server;

public:
    void OnConnect(const PtrConnection &conn)
    {
        DBG_LOG("NEW CONNECTION:%p\n", conn.get());
    }

    void CloseConnect(const PtrConnection &conn)
    {
        DBG_LOG("Release CONNECTION:%p\n", conn.get());
    }

    void OnMessage(const PtrConnection &conn, Buffer *buf)
    {
        conn->Send(buf->GetReadPosion(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        conn->Shutdown();
    }
    
    EchoServer(int port)
        : _server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnect, this, std::placeholders::_1));
        _server.SetClosedCallback(std::bind(&EchoServer::CloseConnect, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    
    void Start()
    {
        _server.Start();
    }
};